% Get equilibrium lambdastar given v0bar and platform structure
% Corresponds to equations 9 (when r=0) and 11 (when r>0). 
% ---------------------------------------------- %
function[nresult] = lambdastarfunc(screeningvalue,v0bar,cP,pibmat,pib0vec,pdfmat,v0vec,zeroreserve,PAR)

    if v0bar < min(v0vec) 
        warning('No entry situation given v0star too low, code as lambdastar=0')
        nresult = 0;
    else
        if Pibidder(cP,screeningvalue,v0bar,0.01,pibmat,pib0vec,pdfmat,v0vec,zeroreserve,PAR) < 0
            nresult     = 0;
        elseif Pibidder(cP,screeningvalue,v0bar,PAR.maxn,pibmat,pib0vec,pdfmat,v0vec,zeroreserve,PAR) > 0
            nresult     = PAR.maxn;
        else
            surplusfunc = @(n) Pibidder(cP,screeningvalue,v0bar,n,pibmat,pib0vec,pdfmat,v0vec,zeroreserve,PAR);
            nresult     = fzero(surplusfunc,[0.01,PAR.maxn],PAR.optslambdastar); %Relatively weak tolerance level - this function gets called often
        end
        
    end  
end
